﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace DZH.TextSearch.BLL
{
    public class DataAccess
    {
        public static string HttpPost(string Url, string postDataStr)
        {

            //try
            //{

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);

                request.Method = "POST";

                request.ContentType = "application/x-www-form-urlencoded";

                //request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);

                Stream myRequestStream = request.GetRequestStream();

                StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb18030"));

                myStreamWriter.Write(postDataStr);

                myStreamWriter.Close();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                //response.Cookies = cookie.GetCookies(response.ResponseUri);

                Stream myResponseStream = response.GetResponseStream();

                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));

                string retString = myStreamReader.ReadToEnd();

                myStreamReader.Close();

                myResponseStream.Close();



                return retString;
            //}
            //catch (WebException ex)
            //{
            //    //MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    throw ex;
            //}

            //return "";
        }



        public static string HttpGet(string Url, string postDataStr)
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);

            request.Method = "GET";

            request.ContentType = "text/html;charset=UTF-8";



            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Stream myResponseStream = response.GetResponseStream();

            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));

            string retString = myStreamReader.ReadToEnd();

            myStreamReader.Close();

            myResponseStream.Close();



            return retString;

        }
    }
}
